﻿@*
//------------------------------------------------------------------------------
//  此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
//  此代码版权（除特别声明外的代码）归作者本人Diego所有
//  源代码使用协议遵循本仓库的开源协议及附加协议
//  Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
//  Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
//  使用文档：https://diego2098.gitee.io/thingsgateway-docs/
//  QQ群：605534569
//------------------------------------------------------------------------------
*@

@page "/gatewayruntime/devicevariable"
@namespace ThingsGateway.Gateway.Blazor
@using System.Linq.Expressions;
@using BlazorComponent;
@using Furion.DataValidation;
@using Furion;
@using Mapster;
@using Masa.Blazor.Presets;
@using System.IO;
@using Masa.Blazor;
@using Microsoft.AspNetCore.Authorization;

@using ThingsGateway.Admin.Blazor;
@using ThingsGateway.Admin.Core;


@attribute [Authorize]
@inherits BaseComponentBase
@inject UserResoures UserResoures
@layout MainLayout
@if (IsMobile)
{
    @GetAppDataTable()

}
else
{
    <MRow>
        <MCol Md=2 Cols="12">
            <MCard Class="ma-2" Height=@("100%")>
                <MCardTitle>
                    <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="mx-2 my-1" @bind-Value="_searchName"
                                Outlined Label=@typeof(CollectDevice).GetDescription(nameof(CollectDevice.DeviceGroup)) />
                </MCardTitle>
                <MTreeview Style=@($"height: calc(100vh - {BlazorResourceConst.DefaultHeight+100}px);overflow-y:auto") Dense TItem="DeviceTree" TKey="string" Active="SearchModel.DeviceName?.StringToList()" OpenOnClick ActiveChanged=@((async a=>
                       {
                       if(this.SearchModel.DeviceName!= a.FirstOrDefault())
                       {
                       this.SearchModel.DeviceName= a.FirstOrDefault();
                       await DatatableQuery();
                       }
                       }) )
                           Items="_deviceGroups" ItemText="r=>r.Name" ItemChildren="r=>r.Childrens"
                           Search="@_searchName"
                          Activatable ItemKey=@(r=>r.Name)>
                    <LabelContent>
                        <span title=@context.Item.Name>
                            @context.Item.Name
                        </span>
                    </LabelContent>
                </MTreeview>

            </MCard>
        </MCol>
        <MCol Md=10 Cols="12">

            @GetAppDataTable()
        </MCol>
    </MRow>
}
@code {
    RenderFragment GetAppDataTable()
    {
        RenderFragment renderFragment =
    @<AppDataTable @ref="_datatable" TItem="DeviceVariableRunTime" SearchItem="VariablePageInput"
                       StyleString=@($"height: calc(100vh - {BlazorResourceConst.DefaultHeight+10}px)")
                       AddItem="object" EditItem="object" IsShowSelect=false DescFunc=@(memberInfo=>memberInfo.GetCustomAttribute<SqlSugar.SugarColumn>(true)?.ColumnDescription)
                       SearchModel="SearchModel" FilterHeaders=FilterHeaders Filters=Filters
                       QueryCallAsync="QueryCallAsync"
                                  IsShowDetailButton IsShowOperCol=true
                                  IsShowQueryButton>
        <SearchTemplate>
            <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1  mx-2 " @bind-Value="context.Name" Clearable
                                Outlined Label=@context.Description(x => x.Name) />
            <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1  mx-2 " @bind-Value="context.VariableAddress" Clearable
                                Outlined Label=@context.Description(x => x.VariableAddress) />
            <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1  mx-2 " @bind-Value="context.DeviceName" Clearable
                                Outlined Label=@context.Description(x => x.DeviceName) />
            <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1  mx-2 " @bind-Value="context.UploadDeviceName" Clearable
                                Outlined Label=@context.Description(x => x.UploadDeviceName) />
        </SearchTemplate>

        <ItemColOperTemplate>
            <MButton Text Disabled=@(!UserResoures.IsHasButtonWithRole("gatewaycollectdeviceedit")) Class="my-1  mx-2" OnClick=@(() => WriteAsync(context.Item))>
                写入
            </MButton>
        </ItemColOperTemplate>
        <ItemColTemplate>
            @switch (context.Header.Value)
            {
                case nameof(context.Item.IsOnline):
                    <EnableChip Value="context.Item.IsOnline" DisabledLabel="离线" EnabledLabel="在线">
                    </EnableChip>
                    break;
                default:
                    @if (context.Header.CellClass?.Contains("text-truncate") == true)
                    {
                        <span title=@context.Value>
                            @context.Value
                        </span>
                    }
                    else
                    {
                        @context.Value
                    }
                    break;

            }

        </ItemColTemplate>
        <Detailemplate>
            @{
                switch (context.Item1.Value)
                {
                    case nameof(DeviceVariable.DeviceId):
                        <tr @key="context.Item1.Text">
                            <td class="text-start table-minwidth">
                                @context.Item1.Text
                            </td>
                            <td class="text-start ">
                                <div style="word-break:break-all; white-space:pre-wrap;">
                                    @(App.GetService<ICollectDeviceService>().GetNameById(context.Item2.ToLong()))
                                </div>
                            </td>
                        </tr>
                        break;
                    default:
                        <tr @key="context.Item1.Text">
                            <td class="text-start table-minwidth">
                                @context.Item1.Text
                            </td>
                            <td class="text-start ">
                                <div style="word-break:break-all; white-space:pre-wrap;">
                                    @context.Item2
                                </div>
                            </td>
                        </tr>
                        break;
                }

            }
        </Detailemplate>
    </AppDataTable>

    ;
        return renderFragment;
    }


}
